<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">


























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.0.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/Peppa.png?v=7.0.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/Peppa.jpg?v=7.0.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/Peppa.jpg?v=7.0.0">








<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '7.0.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="1 前言1.1 计算几何算法ACM各种算法中计算几何算是比较实际的算法，在很多领域有着重要的用途 常用算法包括经典的凸包求解，离散化及扫描线算法、旋转卡壳、半平面交等 1.2 计算几何题目特点及要领 大部分不会很难，少部分题目思路很巧妙  做计算几何题目，模板很重要，模板必须高度可靠  要注意代码的组织，因为计算几何的题目很容易上两百行代码，里面大部分是模板。如果代码一片混乱，那么会严重影响做题正">
<meta name="keywords" content="ACM,计算几何">
<meta property="og:type" content="article">
<meta property="og:title" content="ACM计算几何篇">
<meta property="og:url" content="https://linxi99.gitee.io/20190211/ACM计算几何篇/index.html">
<meta property="og:site_name" content="linxi&#39;s dream">
<meta property="og:description" content="1 前言1.1 计算几何算法ACM各种算法中计算几何算是比较实际的算法，在很多领域有着重要的用途 常用算法包括经典的凸包求解，离散化及扫描线算法、旋转卡壳、半平面交等 1.2 计算几何题目特点及要领 大部分不会很难，少部分题目思路很巧妙  做计算几何题目，模板很重要，模板必须高度可靠  要注意代码的组织，因为计算几何的题目很容易上两百行代码，里面大部分是模板。如果代码一片混乱，那么会严重影响做题正">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://linxi99.gitee.io/20190211/ACM计算几何篇/p1.png">
<meta property="og:image" content="https://linxi99.gitee.io/20190211/ACM计算几何篇/p2.jpeg">
<meta property="og:image" content="https://linxi99.gitee.io/20190211/ACM计算几何篇/p3.jpeg">
<meta property="og:image" content="https://linxi99.gitee.io/20190211/ACM计算几何篇/p4.gif">
<meta property="og:image" content="https://linxi99.gitee.io/20190211/ACM计算几何篇/p5.jpeg">
<meta property="og:image" content="https://linxi99.gitee.io/20190211/ACM计算几何篇/p6.png">
<meta property="og:image" content="https://linxi99.gitee.io/20190211/ACM计算几何篇/p7.jpeg">
<meta property="og:image" content="https://linxi99.gitee.io/20190211/ACM计算几何篇/p8.jpeg">
<meta property="og:updated_time" content="2019-11-25T09:53:05.266Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="ACM计算几何篇">
<meta name="twitter:description" content="1 前言1.1 计算几何算法ACM各种算法中计算几何算是比较实际的算法，在很多领域有着重要的用途 常用算法包括经典的凸包求解，离散化及扫描线算法、旋转卡壳、半平面交等 1.2 计算几何题目特点及要领 大部分不会很难，少部分题目思路很巧妙  做计算几何题目，模板很重要，模板必须高度可靠  要注意代码的组织，因为计算几何的题目很容易上两百行代码，里面大部分是模板。如果代码一片混乱，那么会严重影响做题正">
<meta name="twitter:image" content="https://linxi99.gitee.io/20190211/ACM计算几何篇/p1.png">






  <link rel="canonical" href="https://linxi99.gitee.io/20190211/ACM计算几何篇/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>ACM计算几何篇 | linxi's dream</title>
  






  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?b4099cd89c9f33abab06a03748dac801";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>







  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">linxi's dream</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <p class="site-subtitle">穷且益坚，不坠青云之志</p>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
        </li>
      
    </ul>
  

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://linxi99.gitee.io/20190211/ACM计算几何篇/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="linxi">
      <meta itemprop="description" content="stay hungry, stay foolish">
      <meta itemprop="image" content="/images/star.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="linxi's dream">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">ACM计算几何篇

              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-02-11 17:01:34" itemprop="dateCreated datePublished" datetime="2019-02-11T17:01:34+08:00">2019-02-11</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-11-25 17:53:05" itemprop="dateModified" datetime="2019-11-25T17:53:05+08:00">2019-11-25</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing"><a href="/categories/计算几何瞎暴力/" itemprop="url" rel="index"><span itemprop="name">计算几何瞎暴力</span></a></span>

                
                
              
            </span>
          

          
            
            
              
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
            
                <span class="post-meta-item-text">评论数：</span>
                <a href="/20190211/ACM计算几何篇/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/20190211/ACM计算几何篇/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
            <span id="/20190211/ACM计算几何篇/" class="leancloud_visitors" data-flag-title="ACM计算几何篇">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              
                <span class="post-meta-item-text">阅读次数：</span>
              
                <span class="leancloud-visitors-count"></span>
            </span>
            <span class="post-meta-divider">|
            </span> <span title="post.wordcount">
            <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
            </span>字数： 6.8k
            </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="1-前言"><a href="#1-前言" class="headerlink" title="1 前言"></a>1 前言</h1><h2 id="1-1-计算几何算法"><a href="#1-1-计算几何算法" class="headerlink" title="1.1 计算几何算法"></a>1.1 计算几何算法</h2><p>ACM各种算法中计算几何算是比较实际的算法，在很多领域有着重要的用途</p>
<p>常用算法包括经典的凸包求解，离散化及扫描线算法、旋转卡壳、半平面交等</p>
<h2 id="1-2-计算几何题目特点及要领"><a href="#1-2-计算几何题目特点及要领" class="headerlink" title="1.2 计算几何题目特点及要领"></a>1.2 计算几何题目特点及要领</h2><ul>
<li><p>大部分不会很难，少部分题目思路很巧妙</p>
</li>
<li><p>做计算几何题目，模板很重要，模板必须高度可靠</p>
</li>
<li><p>要注意代码的组织，因为计算几何的题目很容易上两百行代码，里面大部分是模板。如果代码一片混乱，那么会严重影响做题正确率。</p>
</li>
<li><p>注意精度控制</p>
</li>
<li><p>能用整数的地方尽量用整数，要想到扩大数据的方法（扩大一倍，或扩大sqrt2）。因为整数不用考虑浮点误差，而且运算比浮点快</p>
</li>
</ul>
<a id="more"></a>
<h2 id="1-3-预备知识"><a href="#1-3-预备知识" class="headerlink" title="1.3 预备知识"></a>1.3 预备知识</h2><p>见ACM几何基础篇</p>
<p><a href="https://linxi99.gitee.io/20190211/ACM%E5%87%A0%E4%BD%95%E5%9F%BA%E7%A1%80%E7%AF%87/">https://linxi99.gitee.io/20190211/ACM%E5%87%A0%E4%BD%95%E5%9F%BA%E7%A1%80%E7%AF%87/</a></p>
<p><a href="https://blog.csdn.net/linxilinxilinxi/article/details/81750327" target="_blank" rel="noopener">https://blog.csdn.net/linxilinxilinxi/article/details/81750327</a></p>
<p>计算几何将用到大量基础篇中的函数与知识</p>
<h1 id="2-凸包"><a href="#2-凸包" class="headerlink" title="2 凸包"></a>2 凸包</h1><h2 id="2-1-定义"><a href="#2-1-定义" class="headerlink" title="2.1 定义"></a>2.1 定义</h2><h3 id="2-1-1-凸多边形"><a href="#2-1-1-凸多边形" class="headerlink" title="2.1.1 凸多边形"></a>2.1.1 凸多边形</h3><p>过多边形的任意一边做一条直线，如果其他各个顶点都在这条直线的同侧，则把这个多边形叫做凸多边形</p>
<p>凸包求解算法的基础便是凸多边形的定义与性质</p>
<h3 id="2-1-2-凸包"><a href="#2-1-2-凸包" class="headerlink" title="2.1.2 凸包"></a>2.1.2 凸包</h3><p>假设平面上n个点，过某些点作一个多边形，使这个多边形能把所有点都“包”起来。当这个多边形是凸多边形的时候，我们就叫它“凸包”</p>
<p>形象理解：皮筋包裹钉子群</p>
<p><img src="/20190211/ACM计算几何篇/p1.png" alt="凸包"></p>
<h2 id="2-2-颜料配色问题"><a href="#2-2-颜料配色问题" class="headerlink" title="2.2 颜料配色问题"></a>2.2 颜料配色问题</h2><h3 id="2-2-1-问题描述"><a href="#2-2-1-问题描述" class="headerlink" title="2.2.1 问题描述"></a>2.2.1 问题描述</h3><p>假设每种颜料都拥有$(R,G,B)$三种属性，表示该种颜料红色，绿色，与蓝色的化学成分所占的比重</p>
<p>给你若干种已有的不限量的颜料，问是否能够勾兑出目标颜料$(R_0, G_0, B_0)$</p>
<h3 id="2-2-2-问题简化"><a href="#2-2-2-问题简化" class="headerlink" title="2.2.2 问题简化"></a>2.2.2 问题简化</h3><p>若只考虑$(R, G)$这两种颜色</p>
<p>给定$X = (10\%, 35\%), Y = (16\%, 20\%)$</p>
<p>问是否能够配制出$U = (12\%, 30\%)$的燃料</p>
<p>$V = (13\%, 22\%)$这一种呢</p>
<p>如果再给你一种$Z = (7\%, 15\%)$颜料呢</p>
<h3 id="2-2-3-问题抽象"><a href="#2-2-3-问题抽象" class="headerlink" title="2.2.3 问题抽象"></a>2.2.3 问题抽象</h3><p>将每一种颜料映射为二维欧氏空间中的一个点，我们可以将已经给定的颜料与目的颜料在空间中标定出来</p>
<p>经过观察与思考，我们可以发现，一个颜料能够被勾兑出来当且仅当该颜料对应的点，位于以给定颜料所构成的凸包之中</p>
<h3 id="2-2-4-数学抽象"><a href="#2-2-4-数学抽象" class="headerlink" title="2.2.4 数学抽象"></a>2.2.4 数学抽象</h3><p>Given a point set $S = {p_1, \cdots, p_n} \subseteq \varepsilon ^ 2$</p>
<p>Let $\lambda = &lt;\lambda <em> 1, \cdots, \lambda </em> n&gt; \in R ^ n, \lambda <em> 1 + \lambda </em> 2 + \cdots + \lambda <em> n = 1 $ and $min{\lambda </em> 1, \cdots, \lambda _ n} \ge 0$</p>
<p>The point </p>
<p>$p = [p <em> 1, \cdots, p </em> n]\lambda = \lambda <em> 1 p </em> 1 + \cdots + \lambda <em> n p </em> n$</p>
<p>is called a convex combination of S</p>
<h4 id="2-2-4-1-Convex-Combination-And-Affine-Combination"><a href="#2-2-4-1-Convex-Combination-And-Affine-Combination" class="headerlink" title="2.2.4.1 Convex Combination And Affine Combination"></a>2.2.4.1 Convex Combination And Affine Combination</h4><p>凸组合：Convex Combination</p>
<p>两个点的Convex Combination会是这两个点所构成的线段 </p>
<p>仿射组合：Affine Combination</p>
<p>而两个点的Affine Combination将会确定这两个点所对应的那条直线</p>
<h4 id="2-2-4-2-区别与联系"><a href="#2-2-4-2-区别与联系" class="headerlink" title="2.2.4.2 区别与联系"></a>2.2.4.2 区别与联系</h4><p>也就是说Convex Combination 是 Affine Combination的一个子集</p>
<p>原因便是</p>
<p>$min{\lambda <em> 1, \cdots, \lambda </em> n} \ge 0$</p>
<p>这个根据生活实际添加的条件</p>
<h2 id="2-3-构造凸包的初步尝试"><a href="#2-3-构造凸包的初步尝试" class="headerlink" title="2.3 构造凸包的初步尝试"></a>2.3 构造凸包的初步尝试</h2><h3 id="2-3-1-转化思想"><a href="#2-3-1-转化思想" class="headerlink" title="2.3.1 转化思想"></a>2.3.1 转化思想</h3><p>大事化小，小事化了</p>
<h3 id="2-3-2-极性-Extremity"><a href="#2-3-2-极性-Extremity" class="headerlink" title="2.3.2 极性 Extremity"></a>2.3.2 极性 Extremity</h3><p>称最终对点集所构成的凸包有贡献的点具有极性</p>
<p>称其为极点 Extreme Point</p>
<p>通过观察，所谓的这些极点，我们可以找到一条穿过它们的直线，使得点集中的所有点都落在直线的同一侧</p>
<h3 id="2-3-3-基于极点的凸包构造算法"><a href="#2-3-3-基于极点的凸包构造算法" class="headerlink" title="2.3.3 基于极点的凸包构造算法"></a>2.3.3 基于极点的凸包构造算法</h3><p>类比冒泡排序，我们可以逐个判断每一个给定的点是否位于任何三个点所构成的三角形的内部</p>
<p>这样我们可以逐个剔除所有的非极点，从而得到所有的极点，即凸包的解</p>
<p>很容易想到，该算法的时间复杂度$O(n ^ 4)$</p>
<h3 id="2-3-4-基于极边的凸包构造算法"><a href="#2-3-4-基于极边的凸包构造算法" class="headerlink" title="2.3.4 基于极边的凸包构造算法"></a>2.3.4 基于极边的凸包构造算法</h3><h4 id="2-3-4-1-极边"><a href="#2-3-4-1-极边" class="headerlink" title="2.3.4.1 极边"></a>2.3.4.1 极边</h4><p>类比极点的定义相应的我们也可以定义极边(Extreme Edge)，它们具有类似的性质</p>
<p>很容易可以发现凸包边界上的边若取逆时针走向，点集中所有的都位于极边的左侧(ToLeftTest)</p>
<h4 id="2-3-4-2-尝试实现"><a href="#2-3-4-2-尝试实现" class="headerlink" title="2.3.4.2 尝试实现"></a>2.3.4.2 尝试实现</h4><p>枚举所有点所有边，判断其左右位置相对关系，如果所有点都落在该边的某一侧，则该边为极边，其端点为极点</p>
<p>易得，时间复杂度为$O(n ^ 3)$</p>
<h2 id="2-4-更进一步"><a href="#2-4-更进一步" class="headerlink" title="2.4 更进一步"></a>2.4 更进一步</h2><h3 id="2-4-1-减而治之"><a href="#2-4-1-减而治之" class="headerlink" title="2.4.1 减而治之"></a>2.4.1 减而治之</h3><p>类比插入排序</p>
<p>我们可以考虑根据之前数据已经构成一个凸包，当新点来临之后，我们便可以考虑当前的点是否位于多边形内部 isPointInPolygon()</p>
<p>若位于内部或边界之上，我们便可以断定该点对凸包没有贡献</p>
<p>否则，将该点与凸包所构成的两条支撑线(Support-Lines)缝合到已有答案集合之中，并舍弃失去极性的点</p>
<h3 id="2-4-2-转向形式"><a href="#2-4-2-转向形式" class="headerlink" title="2.4.2 转向形式"></a>2.4.2 转向形式</h3><p>Pattern Of Turns</p>
<p>利用每个顶点相对于当前试图加入的点的不同的转向形式，获得支撑线以及不被丢弃的所有点</p>
<h3 id="2-4-3-优点与劣势"><a href="#2-4-3-优点与劣势" class="headerlink" title="2.4.3 优点与劣势"></a>2.4.3 优点与劣势</h3><p>该算法为在线算法，可以动态的添加点，从而改变凸包形式</p>
<p>但其复杂度虽然达到了$O(n ^ 2)$，但还是不尽如人意</p>
<h2 id="2-5-求解凸包的高效算法"><a href="#2-5-求解凸包的高效算法" class="headerlink" title="2.5 求解凸包的高效算法"></a>2.5 求解凸包的高效算法</h2><h3 id="2-5-1-Jarvis-March的步进算法"><a href="#2-5-1-Jarvis-March的步进算法" class="headerlink" title="2.5.1 Jarvis March的步进算法"></a>2.5.1 Jarvis March的步进算法</h3><p>类比选择排序</p>
<h4 id="2-5-1-1-思路分析"><a href="#2-5-1-1-思路分析" class="headerlink" title="2.5.1.1 思路分析"></a>2.5.1.1 思路分析</h4><p>Lowest-Then-Leftmost</p>
<p>纵坐标最小然后横坐标最小的点一定是凸包上的点， 我们将其记为$p _ 0$</p>
<p>从 $p _ 0$ 开始，按逆时针的方向，逐个找凸包上的点，每前进一步找到一个点，所以叫作步进法。</p>
<p>怎么找下一个点呢？利用夹角。假设现在已经找到 ${p <em> 0，p </em> 1，p _ 2}$ 了</p>
<p>要找下一个点：剩下的点分别和 $p <em> 2$ 组成向量，设这个向量与向量$\vec p </em> 1 p _ 2$的夹角为$\beta$。</p>
<p>当 $\beta$ 最小时就是所要求的下一个点了</p>
<p><img src="/20190211/ACM计算几何篇/p2.jpeg" alt="JM步进法"></p>
<h4 id="2-5-1-2-时间复杂度"><a href="#2-5-1-2-时间复杂度" class="headerlink" title="2.5.1.2 时间复杂度"></a>2.5.1.2 时间复杂度</h4><p>$O(nH)$。（其中 n 是点的总个数，H 是凸包上的点的个数） </p>
<p>具有输出敏感性，所花费的时间与输出的凸包的顶点个数有关</p>
<h4 id="2-5-1-3-注意"><a href="#2-5-1-3-注意" class="headerlink" title="2.5.1.3 注意"></a>2.5.1.3 注意</h4><p>找第二个点 $p <em> 1$ 时，因为已经找到的只有 $p </em> 0$ 一个点，所以向量只能和水平线作夹角 $\alpha$，当 $\alpha$ 最小时求得第二个点</p>
<p>共线情况：如果直线 $p <em> 2 p </em> 3$ 上还有一个点 $p _ 4$，即三个点共线</p>
<p>此时由向量$\vec{p <em> 2 p </em> 3}$ 和向量 $\vec{p <em> 2 p </em> 4}$ 产生的两个 $\beta$ 是相同的</p>
<p>我们应该把 $p <em> 3$、$p </em> 4$ 都当做凸包上的点</p>
<p>并且把距离 $p _ 2$ 最远的那个点作为最后搜索到的点，继续找它的下一个连接点</p>
<h3 id="2-5-2-Graham-Scan"><a href="#2-5-2-Graham-Scan" class="headerlink" title="2.5.2 Graham Scan"></a>2.5.2 Graham Scan</h3><h4 id="2-5-2-1-预处理"><a href="#2-5-2-1-预处理" class="headerlink" title="2.5.2.1 预处理"></a>2.5.2.1 预处理</h4><p>Graham扫描的思想和Jarvis步进法类似，也是先找到凸包上的一个点，然后从那个点开始按逆时针方向逐个找凸包上的点，但它不是利用夹角。 </p>
<p><img src="/20190211/ACM计算几何篇/p3.jpeg" alt="Graham预处理"></p>
<p> 把所有点放在二维坐标系中，则纵坐标最小的点一定是凸包上的点，如图中的$p _ 0$。</p>
<p> 把所有点的坐标平移一下，使 $p _ 0$ 作为原点，如上图。</p>
<h4 id="2-5-2-2-具体步骤"><a href="#2-5-2-2-具体步骤" class="headerlink" title="2.5.2.2 具体步骤"></a>2.5.2.2 具体步骤</h4><ol>
<li><p>计算各个点相对于 $p _ 0$ 的幅角 $\alpha$ ，按从小到大的顺序对各个点排序。</p>
</li>
<li><p>当 $\alpha$ 相同时，距离 $p <em> 0$ 比较近的排在前面。例如上图得到的结果为 $p </em> 1$，$p <em> 2$，$p </em> 3$，$p <em> 4$，$p </em> 5$，$p <em> 6$，$p </em> 7$，$p _ 8$。</p>
</li>
<li><p>我们由几何知识可以知道，结果中第一个点 $p <em> 1$ 和最后一个点 $p </em> 8$ 一定是凸包上的点。<br>（以上是准备步骤，以下开始求凸包）<br>以上，我们已经知道了凸包上的第一个点 $p <em> 0$ 和第二个点 $p </em> 1$，我们把它们放在栈里面。<br>现在从按照极角排好的集合里，把 $p <em> 1$ 后面的那个点拿出来做当前点，即 $p </em> 2$ 。接下来开始找第三个点：</p>
</li>
<li><p>连接栈顶的点与次栈顶的点，得到直线 $l$ 。看当前点是在直线 $l$ 的右边还是左边。<br>如果在直线的右边就执行步骤5；<br>如果在直线上，或者在直线的左边就执行步骤6。</p>
</li>
<li><p>如果在右边，则栈顶的那个元素不是凸包上的点，把栈顶元素出栈。执行步骤4。</p>
</li>
<li>当前点是凸包上的点，把它压入栈，执行步骤7。</li>
<li>检查当前的点 $p <em> i$ 是不是步骤3那个结果的最后一个元素。是最后一个元素的话就结束。如果不是的话就把 $p </em> i$ 后面那个点做当前点，返回步骤4。</li>
</ol>
<p>最后，栈中的元素就是凸包上的点了。 </p>
<p>以下为用 GrahamScan 动态求解的过程： </p>
<p>大家可以直观的了解一下</p>
<p><img src="/20190211/ACM计算几何篇/p4.gif" alt="Graham Scan"></p>
<h4 id="2-5-2-3-时间复杂度"><a href="#2-5-2-3-时间复杂度" class="headerlink" title="2.5.2.3 时间复杂度"></a>2.5.2.3 时间复杂度</h4><p>根据Scan过程的步骤分析，我们可以得到其时间复杂度为$O(n)$</p>
<p>但由于其预处理排序时间复杂度为$O(nlogn)$</p>
<p>故其总的时间复杂度为$O(nlogn)$</p>
<h4 id="2-5-2-4-代码实现"><a href="#2-5-2-4-代码实现" class="headerlink" title="2.5.2.4 代码实现"></a>2.5.2.4 代码实现</h4><p>根据实现难度，对上述步骤做了些许修改</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> maxn = <span class="number">1e3</span> + <span class="number">5</span>;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span> &#123;</span></span><br><span class="line">    <span class="keyword">double</span> x, y;</span><br><span class="line">    Point(<span class="keyword">double</span> x = <span class="number">0</span>, <span class="keyword">double</span> y = <span class="number">0</span>):x(x),y(y)&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">typedef</span> Point Vector;</span><br><span class="line">Point lst[maxn];</span><br><span class="line"><span class="keyword">int</span> stk[maxn], top;</span><br><span class="line">Vector <span class="keyword">operator</span> - (Point A, Point B)&#123;</span><br><span class="line">    <span class="keyword">return</span> Vector(A.x-B.x, A.y-B.y);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">sgn</span><span class="params">(<span class="keyword">double</span> x)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">fabs</span>(x) &lt; eps)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span>(x &lt; <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Cross</span><span class="params">(Vector v0, Vector v1)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> v0.x*v1.y - v1.x*v0.y;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Dis</span><span class="params">(Point p1, Point p2)</span> </span>&#123; <span class="comment">//计算 p1p2的 距离</span></span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">sqrt</span>((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">cmp</span><span class="params">(Point p1, Point p2)</span> </span>&#123; <span class="comment">//极角排序函数 ，角度相同则距离小的在前面</span></span><br><span class="line">    <span class="keyword">int</span> tmp = sgn(Cross(p1 - lst[<span class="number">0</span>], p2 - lst[<span class="number">0</span>]));</span><br><span class="line">    <span class="keyword">if</span>(tmp &gt; <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">if</span>(tmp == <span class="number">0</span> &amp;&amp; Dis(lst[<span class="number">0</span>], p1) &lt; Dis(lst[<span class="number">0</span>], p2))</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//点的编号0 ~ n - 1</span></span><br><span class="line"><span class="comment">//返回凸包结果stk[0 ~ top - 1]为凸包的编号</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Graham</span><span class="params">(<span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> k = <span class="number">0</span>;</span><br><span class="line">    Point p0;</span><br><span class="line">    p0.x = lst[<span class="number">0</span>].x;</span><br><span class="line">    p0.y = lst[<span class="number">0</span>].y;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n; ++i) &#123;</span><br><span class="line">        <span class="keyword">if</span>( (p0.y &gt; lst[i].y) || ((p0.y == lst[i].y) &amp;&amp; (p0.x &gt; lst[i].x)) ) &#123;</span><br><span class="line">            p0.x = lst[i].x;</span><br><span class="line">            p0.y = lst[i].y;</span><br><span class="line">            k = i;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    lst[k] = lst[<span class="number">0</span>];</span><br><span class="line">    lst[<span class="number">0</span>] = p0;</span><br><span class="line">    sort(lst + <span class="number">1</span>, lst + n, cmp);</span><br><span class="line">    <span class="keyword">if</span>(n == <span class="number">1</span>) &#123;</span><br><span class="line">        top = <span class="number">1</span>;</span><br><span class="line">        stk[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">return</span> ;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(n == <span class="number">2</span>) &#123;</span><br><span class="line">        top = <span class="number">2</span>;</span><br><span class="line">        stk[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">        stk[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">return</span> ;</span><br><span class="line">    &#125;</span><br><span class="line">    stk[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">    stk[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line">    top = <span class="number">2</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i &lt; n; ++i) &#123;</span><br><span class="line">        <span class="keyword">while</span>(top &gt; <span class="number">1</span> &amp;&amp; Cross(lst[stk[top - <span class="number">1</span>]] - lst[stk[top - <span class="number">2</span>]], lst[i] - lst[stk[top - <span class="number">2</span>]]) &lt;= <span class="number">0</span>)</span><br><span class="line">            --top;</span><br><span class="line">        stk[top] = i;</span><br><span class="line">        ++top;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> ;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="2-5-3-Andrew算法"><a href="#2-5-3-Andrew算法" class="headerlink" title="2.5.3 Andrew算法"></a>2.5.3 Andrew算法</h3><h4 id="2-5-3-1-由来"><a href="#2-5-3-1-由来" class="headerlink" title="2.5.3.1 由来"></a>2.5.3.1 由来</h4><p>Andrew算法是GrahamScan算法的变种</p>
<p>和原始的GrahamScan算法相比，Andrew更快，且数值稳定性更好</p>
<h4 id="2-5-3-2-预处理"><a href="#2-5-3-2-预处理" class="headerlink" title="2.5.3.2 预处理"></a>2.5.3.2 预处理</h4><p>基于水平排序</p>
<p>首先把所有点按照 Leftmost Then Lowest 原则排序，删除重复点后得到序列$p <em> 1, p </em> 2\cdots $</p>
<p>然后把$p <em> 1$和$p </em> 2$放到凸包中。</p>
<h4 id="2-5-3-3-算法步骤"><a href="#2-5-3-3-算法步骤" class="headerlink" title="2.5.3.3 算法步骤"></a>2.5.3.3 算法步骤</h4><p>从$p _ 3$开始，当新点在凸包的“前进”方向的左边时继续，否则依次删除最近加入凸包的点，直到新点在左边</p>
<p>重复此过程，直到碰到最右边的$p <em> n$，就求出了“下凸包”。然后反过来从$p </em> n$开始再做一次，求出“上凸包”，合并起来就是完整的凸包</p>
<h4 id="2-5-3-4-时间复杂度"><a href="#2-5-3-4-时间复杂度" class="headerlink" title="2.5.3.4 时间复杂度"></a>2.5.3.4 时间复杂度</h4><p>两次扫描均为$O(n)$</p>
<p>预处理排序的时间复杂度为$O(nlogn)$</p>
<p>总时间复杂度为$O(nlogn)$</p>
<h4 id="2-5-3-5-代码实现"><a href="#2-5-3-5-代码实现" class="headerlink" title="2.5.3.5 代码实现"></a>2.5.3.5 代码实现</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span> &#123;</span></span><br><span class="line">    <span class="keyword">double</span> x, y;</span><br><span class="line">    Point(<span class="keyword">double</span> x = <span class="number">0</span>, <span class="keyword">double</span> y = <span class="number">0</span>):x(x),y(y)&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">typedef</span> Point Vector;</span><br><span class="line">Vector <span class="keyword">operator</span> - (Point A, Point B)&#123;</span><br><span class="line">    <span class="keyword">return</span> Vector(A.x-B.x, A.y-B.y);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">bool</span> <span class="keyword">operator</span> &lt; (<span class="keyword">const</span> Point&amp; a, <span class="keyword">const</span> Point&amp; b)&#123;</span><br><span class="line">    <span class="keyword">if</span>(a.x == b.x)</span><br><span class="line">        <span class="keyword">return</span> a.y &lt; b.y;</span><br><span class="line">    <span class="keyword">return</span> a.x &lt; b.x;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Cross</span><span class="params">(Vector v0, Vector v1)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> v0.x*v1.y - v1.x*v0.y;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//计算凸包，输入点数组为 p，个数为 n， 输出点数组为 ch。函数返回凸包顶点数</span></span><br><span class="line"><span class="comment">//如果不希望凸包的边上有输入点，则把两个 &lt;= 改为 &lt;</span></span><br><span class="line"><span class="comment">//在精度要求高时建议用dcmp比较</span></span><br><span class="line"><span class="comment">//输入不能有重复点，函数执行完后输入点的顺序被破坏</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">ConvexHull</span><span class="params">(Point* p, <span class="keyword">int</span> n, Point* ch)</span> </span>&#123;</span><br><span class="line">    sort(p, p+n);</span><br><span class="line">    <span class="keyword">int</span> m = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; ++i) &#123;</span><br><span class="line">        <span class="keyword">while</span>(m &gt; <span class="number">1</span> &amp;&amp; Cross(ch[m<span class="number">-1</span>] - ch[m<span class="number">-2</span>], p[i] - ch[m<span class="number">-2</span>]) &lt; <span class="number">0</span>) &#123;</span><br><span class="line">            m--;</span><br><span class="line">        &#125;</span><br><span class="line">        ch[m++] = p[i];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">int</span> k = m;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = n<span class="number">-2</span>; i&gt;= <span class="number">0</span>; --i) &#123;</span><br><span class="line">        <span class="keyword">while</span>(m &gt; k &amp;&amp; Cross(ch[m<span class="number">-1</span>] - ch[m<span class="number">-2</span>], p[i] - ch[m<span class="number">-2</span>]) &lt; <span class="number">0</span>) &#123;</span><br><span class="line">            m--;</span><br><span class="line">        &#125;</span><br><span class="line">        ch[m++] = p[i];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(n &gt; <span class="number">1</span>)</span><br><span class="line">        --m;</span><br><span class="line">    <span class="keyword">return</span> m;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="2-5-4-分治法求解凸包"><a href="#2-5-4-分治法求解凸包" class="headerlink" title="2.5.4 分治法求解凸包"></a>2.5.4 分治法求解凸包</h3><h3 id="2-5-5-Melkman算法"><a href="#2-5-5-Melkman算法" class="headerlink" title="2.5.5 Melkman算法"></a>2.5.5 Melkman算法</h3><h1 id="3-离散化"><a href="#3-离散化" class="headerlink" title="3 离散化"></a>3 离散化</h1><h2 id="3-1-概述"><a href="#3-1-概述" class="headerlink" title="3.1 概述"></a>3.1 概述</h2><p>与其说离散化是一种算法，不如说是一种程序设计中的非常常用的技巧，它可以有效的降低时间复杂度</p>
<p>离散化不仅在计算几何中经常用到，它几乎和所有算法都能结合成为考点</p>
<h2 id="3-2-基本思想"><a href="#3-2-基本思想" class="headerlink" title="3.2 基本思想"></a>3.2 基本思想</h2><p>在众多可能的情况中只考虑我需要用的值</p>
<h2 id="3-3-例题：区域的个数"><a href="#3-3-例题：区域的个数" class="headerlink" title="3.3 例题：区域的个数"></a>3.3 例题：区域的个数</h2><h3 id="3-3-1-题目描述"><a href="#3-3-1-题目描述" class="headerlink" title="3.3.1 题目描述"></a>3.3.1 题目描述</h3><p>$w \times h$ 的的格子画了$n$条或垂直或水平宽度为1的直线，求出这些格子被划分成了多少个4连块（上、下、左、右连通）。</p>
<p><img src="/20190211/ACM计算几何篇/p5.jpeg" alt="区域的个数"></p>
<p>【输入格式】</p>
<p>　　第一行包含两个整数：w和h，表示矩形的列数和行数（行列编号都从1开始）。<br>　　第二行包含一个整数n，表示有n条直线。<br>　　接下来的n行，每行包含四个整数：x1,y1,x2,y2，表示一条直线的列号和行号。</p>
<p>【输出格式】</p>
<p>　　一个整数，表示区域数量。</p>
<p>【输入样例】</p>
<p>10 10<br>5<br>1 4 6 4<br>1 8 10 8<br>4 1 4 10<br>9 1 9 5<br>10 6 10 10</p>
<p>【输出样例】</p>
<p>6</p>
<p>【数据范围】</p>
<p>1&lt;=w,h&lt;=1000000 , 1&lt;=n&lt;=500</p>
<h3 id="3-3-2-思路分析"><a href="#3-3-2-思路分析" class="headerlink" title="3.3.2 思路分析"></a>3.3.2 思路分析</h3><p>准备好$w \times h$的数组，并记录是否有直线通过，然后一通$bfs$或者$dfs$就可以解决战斗</p>
<p>但这个问题中$w$和$h$最大为1000000，所以没有办法创建$w \times h$的数组</p>
<p>因此我们需要使用坐标离散化这一技巧</p>
<p><img src="/20190211/ACM计算几何篇/p6.png" alt="坐标离散化"></p>
<p>如上图所示，将前后没有变化的行列消除后并不会影响区域的个数</p>
<p>数组里只需要存储有直线的行列以及其前后的行列就足够了，这样的话大小最多$6n \times 6n$，因此就可以创建出数组并利用搜索求出区域的个数</p>
<h3 id="3-3-3-代码实现"><a href="#3-3-3-代码实现" class="headerlink" title="3.3.3 代码实现"></a>3.3.3 代码实现</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> maxn = <span class="number">5e2</span> + <span class="number">5</span>;</span><br><span class="line"><span class="keyword">int</span> x1[maxn], x2[maxn], y1[maxn], y2[maxn];<span class="comment">//开始列号结束列号，开始行号，结束行号</span></span><br><span class="line"><span class="keyword">int</span> w, h, n, ans;<span class="comment">//宽，高，以及横线的个数</span></span><br><span class="line"><span class="keyword">bool</span> line[maxn*<span class="number">6</span>][maxn*<span class="number">6</span>];</span><br><span class="line"><span class="keyword">int</span> dire[<span class="number">5</span>][<span class="number">3</span>]= &#123;&#123;<span class="number">-1</span>, <span class="number">0</span>&#125;, &#123;<span class="number">1</span>, <span class="number">0</span>&#125;, &#123;<span class="number">0</span>, <span class="number">-1</span>&#125;, &#123;<span class="number">0</span>, <span class="number">1</span>&#125;&#125;;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span> &#123;</span></span><br><span class="line">    <span class="keyword">int</span> x, y;</span><br><span class="line">    node(<span class="keyword">int</span> x, <span class="keyword">int</span> y):x(x), y(y) &#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//对x1和x2进行离散化，并返回离散化之后的宽度</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">compress</span><span class="params">(<span class="keyword">int</span> *xx1,<span class="keyword">int</span> *xx2,<span class="keyword">int</span> w)</span> </span>&#123; <span class="comment">//开始坐标，结束坐标</span></span><br><span class="line">    <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; v;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; ++i) <span class="comment">//将横线本身以及附近两横线存储</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> d = <span class="number">-1</span>; d &lt;= <span class="number">1</span>; ++d) &#123;</span><br><span class="line">            <span class="keyword">int</span> nx1 = xx1[i] + d;</span><br><span class="line">            <span class="keyword">int</span> nx2 = xx2[i] + d;</span><br><span class="line">            <span class="keyword">if</span>(nx1 &gt;= <span class="number">1</span> &amp;&amp; nx1 &lt;= w) v.push_back(nx1);</span><br><span class="line">            <span class="keyword">if</span>(nx2 &gt;= <span class="number">1</span> &amp;&amp; nx2 &lt;= w) v.push_back(nx2);</span><br><span class="line">        &#125;</span><br><span class="line">    <span class="comment">//去重</span></span><br><span class="line">    sort(v.begin(), v.end());</span><br><span class="line">    v.erase(unique(v.begin(),v.end()),v.end());</span><br><span class="line">    <span class="comment">//unique()函数去重并返回多余元素存储的起始位置</span></span><br><span class="line">    <span class="comment">//erase()函数区间删除左开右闭的区间</span></span><br><span class="line">    <span class="comment">//离散化后的坐标</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; ++i) &#123;</span><br><span class="line">        xx1[i] = find(v.begin(), v.end(), xx1[i]) - v.begin();</span><br><span class="line">        xx2[i] = find(v.begin(), v.end(), xx2[i]) - v.begin();</span><br><span class="line">        <span class="comment">//通过find()函数将坐标转化为了下标</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> v.size();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">bfs</span><span class="params">(<span class="keyword">int</span> xx,<span class="keyword">int</span> yy)</span> </span>&#123;</span><br><span class="line">    <span class="built_in">queue</span>&lt;node&gt; q;</span><br><span class="line">    q.push(node&#123;xx, yy&#125;);</span><br><span class="line">    line[xx][yy] = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span>(!q.empty()) &#123;</span><br><span class="line">        node t=q.front();</span><br><span class="line">        q.pop();</span><br><span class="line">        <span class="keyword">int</span> x=t.x;</span><br><span class="line">        <span class="keyword">int</span> y=t.y;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;<span class="number">4</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> nx=x+dire[i][<span class="number">0</span>];</span><br><span class="line">            <span class="keyword">int</span> ny=y+dire[i][<span class="number">1</span>];</span><br><span class="line">            <span class="keyword">if</span>(nx&lt;<span class="number">0</span>||nx&gt;=h||ny&lt;<span class="number">0</span>||ny&gt;=w)<span class="keyword">continue</span>;</span><br><span class="line">            <span class="keyword">if</span>(!line[nx][ny]) &#123;</span><br><span class="line">                line[nx][ny]=<span class="number">1</span>;</span><br><span class="line">                q.push(node(nx, ny));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> ;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">while</span>(<span class="built_in">cin</span> &gt;&gt; w &gt;&gt; h &gt;&gt; n) &#123;</span><br><span class="line">        ans = <span class="number">0</span>;</span><br><span class="line">        <span class="built_in">memset</span>(line, <span class="literal">false</span>, <span class="keyword">sizeof</span>(line));</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; ++i) <span class="built_in">cin</span> &gt;&gt; x1[i];</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; ++i) <span class="built_in">cin</span> &gt;&gt; x2[i];</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; ++i) <span class="built_in">cin</span> &gt;&gt; y1[i];</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; ++i) <span class="built_in">cin</span> &gt;&gt; y2[i];</span><br><span class="line"></span><br><span class="line">        w = compress(x1, x2, w);</span><br><span class="line">        h = compress(y1, y2, h);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//标记上所在横线上的点</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; ++i) <span class="comment">//枚举n条横线</span></span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> y = y1[i]; y &lt;= y2[i]; ++y) <span class="comment">//枚举行</span></span><br><span class="line">                <span class="keyword">for</span>(<span class="keyword">int</span> x = x1[i]; x &lt;= x2[i]; ++x) <span class="comment">//枚举列</span></span><br><span class="line">                    line[y][x] = <span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//打印查看离散化后的图形</span></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        for(int i=0;i&lt;h;i++)</span></span><br><span class="line"><span class="comment">        &#123;</span></span><br><span class="line"><span class="comment">        	for(int j=0;j&lt;w;j++)</span></span><br><span class="line"><span class="comment">        	&#123;</span></span><br><span class="line"><span class="comment">        		if(line[i][j])cout&lt;&lt;1&lt;&lt;" ";</span></span><br><span class="line"><span class="comment">        		else cout&lt;&lt;0&lt;&lt;" ";</span></span><br><span class="line"><span class="comment">         &#125;</span></span><br><span class="line"><span class="comment">         cout&lt;&lt;endl;</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">        cout&lt;&lt;endl&lt;&lt;ans&lt;&lt;endl;</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">        <span class="comment">//搜索求区域块数 防止爆栈，这里使用广搜</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; h; ++i)&#123;</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; w; ++j) &#123;</span><br><span class="line">                <span class="keyword">if</span>(!line[i][j]) &#123;</span><br><span class="line">                    ++ans;</span><br><span class="line">                    bfs(i,j);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">cout</span>&lt;&lt;ans&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">10 10 5</span></span><br><span class="line"><span class="comment">1 1 4 9 10</span></span><br><span class="line"><span class="comment">6 10 4 9 10</span></span><br><span class="line"><span class="comment">4 8 1 1 6</span></span><br><span class="line"><span class="comment">4 8 10 5 10</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h1 id="4-扫描线法（平面扫描）"><a href="#4-扫描线法（平面扫描）" class="headerlink" title="4 扫描线法（平面扫描）"></a>4 扫描线法（平面扫描）</h1><h2 id="4-1-描述"><a href="#4-1-描述" class="headerlink" title="4.1 描述"></a>4.1 描述</h2><p>在集合问题中，我们经常利用平面扫描技术来降低算法的复杂度</p>
<p>所谓平面扫描，是指扫描线在平面上按给定轨迹移动的同时，不断根据扫描线扫过部分更新信息，从而得到整体所要求的结果的方法</p>
<p>扫描的方法，既可以从左向右与$y$轴平行的直线，也可以固定射线的端点逆时针转动</p>
<h2 id="4-2-例题：矩形面积并"><a href="#4-2-例题：矩形面积并" class="headerlink" title="4.2 例题：矩形面积并"></a>4.2 例题：矩形面积并</h2><h3 id="4-2-1-题目描述"><a href="#4-2-1-题目描述" class="headerlink" title="4.2.1 题目描述"></a>4.2.1 题目描述</h3><p>给出$n$个矩形的左下角和右上角的坐标，求矩形面积的并</p>
<p><img src="/20190211/ACM计算几何篇/p7.jpeg" alt="转化前"></p>
<p><img src="/20190211/ACM计算几何篇/p8.jpeg" alt="转化后"></p>
<p>【输入格式】</p>
<p>　　第一行包含一个整数：n，表示矩形数量。<br>　　第$2$到$n + 1$行，每行包含四个浮点数，表示该矩形左下角与右上角的坐标</p>
<p>【输出格式】</p>
<p>　　矩形面积交的大小</p>
<p>【输入样例】<br>　　2<br>　　10 10 20 20<br>　　5 15 25 25.5<br>【输出样例】</p>
<p>　　180.00</p>
<p>【数据范围】</p>
<p>1 &lt;= n &lt;= 100<br>0 &lt;= x1 &lt; x2 &lt;= 100000，0 &lt;= y1 &lt; y2 &lt;= 100000</p>
<h3 id="4-2-2-思路分析"><a href="#4-2-2-思路分析" class="headerlink" title="4.2.2 思路分析"></a>4.2.2 思路分析</h3><p>首先，矩形比较多，坐标也很大，所以横坐标需要离散化（纵坐标不需要离散化）</p>
<p>//不离散化直接线段树维护也可以</p>
<p>考虑一条扫描线，从最下方竖直向上扫描</p>
<p>扫描前我们需要保存好矩形的上下边，并且按照高度进行排序，</p>
<p>另外如果该边为矩形的上边则将其标记为-1</p>
<p>否则将其标记为1</p>
<p>接着让扫描线从下往上扫描，每遇到一条上下边就停下来，将这条线段投影到总区间上</p>
<p>这个投影对应的其实就是插入和删除线段的操作</p>
<p>下边是1，扫描到下边的话相当于往总区间插入一条线段</p>
<p>上边是-1，扫描到上边的话相当于在总区间删除一条线段</p>
<p>每次扫描到一条上下边后，就区间求和，得到现在被覆盖的总长度，乘其扫过的高度并将其统计起来</p>
<p>以此往复，当扫出最高的那条边，我们便获得了所需求得矩形面积交</p>
<h2 id="4-3-寻找平面线段交点O-nlogn"><a href="#4-3-寻找平面线段交点O-nlogn" class="headerlink" title="4.3 寻找平面线段交点O(nlogn)"></a>4.3 寻找平面线段交点O(nlogn)</h2><h1 id="5-半平面交"><a href="#5-半平面交" class="headerlink" title="5 半平面交"></a>5 半平面交</h1><h2 id="5-1-描述"><a href="#5-1-描述" class="headerlink" title="5.1 描述"></a>5.1 描述</h2><p>简单地说，半平面交问题就是给出若干个半平面，求他们的公共部分。</p>
<p>其中每个半平面都用一条有向线段表示，它的左侧就是它所代表的半平面</p>
<h2 id="5-2-有向线段"><a href="#5-2-有向线段" class="headerlink" title="5.2 有向线段"></a>5.2 有向线段</h2><p>代码实现</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Line</span>&#123;</span></span><br><span class="line">    Point p;<span class="comment">//直线上任意一点</span></span><br><span class="line">    Vector v;<span class="comment">//方向向量，它的左边就是对应的半平面</span></span><br><span class="line">    <span class="keyword">double</span> ang;<span class="comment">//极角，即从x轴正半轴旋转到向量v所需要的角（弧度）</span></span><br><span class="line">    Line()&#123;&#125;</span><br><span class="line">    Line(Point p, Vector v) : p(p), v(v)&#123;</span><br><span class="line">        ang = <span class="built_in">atan2</span>(v.y, v.x);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">bool</span> <span class="keyword">operator</span> &lt; (<span class="keyword">const</span> Line&amp; L) <span class="keyword">const</span> &#123;<span class="comment">//排序用的比较运算符</span></span><br><span class="line">        <span class="keyword">return</span> ang &lt; L.ang;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="5-3-半平面交的结果"><a href="#5-3-半平面交的结果" class="headerlink" title="5.3 半平面交的结果"></a>5.3 半平面交的结果</h2><p>在很多情况下，半平面交的结果都是一个凸多边形</p>
<p>但也有时候会得到一个无界多边形</p>
<p>甚至是一条直线、线段或者是点</p>
<p>但不管怎样，结果一定是凸的（凸集的交是凸的）</p>
<p>当然，半平面交也可以为空</p>
<h2 id="5-4-计算方法"><a href="#5-4-计算方法" class="headerlink" title="5.4 计算方法"></a>5.4 计算方法</h2><h3 id="5-4-1-增量法"><a href="#5-4-1-增量法" class="headerlink" title="5.4.1 增量法"></a>5.4.1 增量法</h3><p>初始答案为整个平面</p>
<p>然后逐一的加入各个半平面，维护当前的半平面交</p>
<p>为了编程方便，我们一般用一个很大的矩形（4个半平面的交）代替“整个平面”</p>
<p>计算出结果以后再删去这四个人工半平面</p>
<p>这样，没加入一个平面就相当于用一条有向直线去切割多边形</p>
<h3 id="5-4-2-切割方法"><a href="#5-4-2-切割方法" class="headerlink" title="5.4.2 切割方法"></a>5.4.2 切割方法</h3><p>按照逆时针顺序考虑多边形所有的顶点</p>
<p>保留在直线左侧和直线上的点，而删除直线右边的点</p>
<p>如果有向直线和多边形相交时产生了新的点，这些点应该加在新的多边形中</p>
<h3 id="5-4-3-时间复杂度"><a href="#5-4-3-时间复杂度" class="headerlink" title="5.4.3 时间复杂度"></a>5.4.3 时间复杂度</h3><p>每次遍历切割的时间复杂度为$O(n)$</p>
<p>但我们可以通过双端队列优化加扫描的方式将每次切割的成本平摊为$O(logn)$</p>
<p>因此半平面交可以在$O(nlogn)$的时间内解决</p>
<h3 id="5-4-4-代码实现"><a href="#5-4-4-代码实现" class="headerlink" title="5.4.4 代码实现"></a>5.4.4 代码实现</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> eps = <span class="number">1e-6</span>;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span>&#123;</span></span><br><span class="line">    <span class="keyword">double</span> x, y;</span><br><span class="line">    Point(<span class="keyword">double</span> x = <span class="number">0</span>, <span class="keyword">double</span> y = <span class="number">0</span>):x(x),y(y)&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">typedef</span> Point Vector;</span><br><span class="line">Vector <span class="keyword">operator</span> + (Vector A, Vector B)&#123;</span><br><span class="line">    <span class="keyword">return</span> Vector(A.x+B.x, A.y+B.y);</span><br><span class="line">&#125;</span><br><span class="line">Vector <span class="keyword">operator</span> - (Point A, Point B)&#123;</span><br><span class="line">    <span class="keyword">return</span> Vector(A.x-B.x, A.y-B.y);</span><br><span class="line">&#125;</span><br><span class="line">Vector <span class="keyword">operator</span> * (Vector A, <span class="keyword">double</span> p)&#123;</span><br><span class="line">    <span class="keyword">return</span> Vector(A.x*p, A.y*p);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">sgn</span><span class="params">(<span class="keyword">double</span> x)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">fabs</span>(x) &lt; eps)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span>(x &lt; <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Dot</span><span class="params">(Vector A, Vector B)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> A.x*B.x + A.y*B.y;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Cross</span><span class="params">(Vector A, Vector B)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> A.x*B.y-A.y*B.x;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Length</span><span class="params">(Vector A)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">sqrt</span>(Dot(A, A));</span><br><span class="line">&#125;</span><br><span class="line"><span class="function">Vector <span class="title">Normal</span><span class="params">(Vector A)</span></span>&#123;<span class="comment">//向量A左转90°的单位法向量</span></span><br><span class="line">    <span class="keyword">double</span> L = Length(A);</span><br><span class="line">    <span class="keyword">return</span> Vector(-A.y/L, A.x/L);</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Line</span>&#123;</span></span><br><span class="line">    Point p;<span class="comment">//直线上任意一点</span></span><br><span class="line">    Vector v;<span class="comment">//方向向量，它的左边就是对应的半平面</span></span><br><span class="line">    <span class="keyword">double</span> ang;<span class="comment">//极角，即从x轴正半轴旋转到向量v所需要的角（弧度）</span></span><br><span class="line">    Line()&#123;&#125;</span><br><span class="line">    Line(Point p, Vector v) : p(p), v(v)&#123;</span><br><span class="line">        ang = <span class="built_in">atan2</span>(v.y, v.x);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">bool</span> <span class="keyword">operator</span> &lt; (<span class="keyword">const</span> Line&amp; L) <span class="keyword">const</span> &#123;<span class="comment">//排序用的比较运算符</span></span><br><span class="line">        <span class="keyword">return</span> ang &lt; L.ang;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//点p在有向直线L的左侧</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">OnLeft</span><span class="params">(Line L, Point p)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> Cross(L.v, p - L.p) &gt; <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//两直线交点。假定交点唯一存在</span></span><br><span class="line"><span class="function">Point <span class="title">GetIntersection</span><span class="params">(Line a, Line b)</span></span>&#123;</span><br><span class="line">    Vector u = a.p - b.p;</span><br><span class="line">    <span class="keyword">double</span> t = Cross(b.v, u)/Cross(a.v, b.v);</span><br><span class="line">    <span class="keyword">return</span> a.p + a.v*t;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//半平面交的主过程</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">HalfplaneIntersection</span><span class="params">(Line* L, <span class="keyword">int</span> n, Point* poly)</span></span>&#123;</span><br><span class="line">    sort(L, L + n);<span class="comment">//按照极角排序</span></span><br><span class="line">    <span class="keyword">int</span> fst = <span class="number">0</span>, lst = <span class="number">0</span>;<span class="comment">//双端队列的第一个元素和最后一个元素</span></span><br><span class="line">    Point *P = <span class="keyword">new</span> Point[n];<span class="comment">//p[i] 为 q[i]与q[i + 1]的交点</span></span><br><span class="line">    Line *q = <span class="keyword">new</span> Line[n];<span class="comment">//双端队列</span></span><br><span class="line">    q[fst = lst = <span class="number">0</span>] = L[<span class="number">0</span>];<span class="comment">//初始化为只有一个半平面L[0]</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n; ++i)&#123;</span><br><span class="line">        <span class="keyword">while</span>(fst &lt; lst &amp;&amp; !OnLeft(L[i], P[lst - <span class="number">1</span>])) --lst;</span><br><span class="line">        <span class="keyword">while</span>(fst &lt; lst &amp;&amp; !OnLeft(L[i], P[fst])) ++fst;</span><br><span class="line">        q[++lst] = L[i];</span><br><span class="line">        <span class="keyword">if</span>(sgn(Cross(q[lst].v, q[lst - <span class="number">1</span>].v)) == <span class="number">0</span>)&#123;</span><br><span class="line">            <span class="comment">//两向量平行且同向，取内侧一个</span></span><br><span class="line">            --lst;</span><br><span class="line">            <span class="keyword">if</span>(OnLeft(q[lst], L[i].p)) q[lst] = L[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(fst &lt; lst)</span><br><span class="line">            P[lst - <span class="number">1</span>] = GetIntersection(q[lst - <span class="number">1</span>], q[lst]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(fst &lt; lst &amp;&amp; !OnLeft(q[fst], P[lst - <span class="number">1</span>])) --lst;</span><br><span class="line">    <span class="comment">//删除无用平面</span></span><br><span class="line">    <span class="keyword">if</span>(lst - fst &lt;= <span class="number">1</span>) <span class="keyword">return</span> <span class="number">0</span>;<span class="comment">//空集</span></span><br><span class="line">    P[lst] = GetIntersection(q[lst], q[fst]);<span class="comment">//计算首尾两个半平面的交点</span></span><br><span class="line">    <span class="comment">//从deque复制到输出中</span></span><br><span class="line">    <span class="keyword">int</span> m = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = fst; i &lt;= lst; ++i) poly[m++] = P[i];</span><br><span class="line">    <span class="keyword">return</span> m;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="6-分治法解决平面最近点对（O-nlogn-）"><a href="#6-分治法解决平面最近点对（O-nlogn-）" class="headerlink" title="6 分治法解决平面最近点对（O(nlogn)）"></a>6 分治法解决平面最近点对（O(nlogn)）</h1><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> maxn = <span class="number">2e5</span> + <span class="number">5</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> inf = <span class="number">1e10</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span> &#123;</span></span><br><span class="line">    <span class="keyword">double</span> x,y;</span><br><span class="line">    <span class="keyword">bool</span> <span class="keyword">operator</span> &lt;(<span class="keyword">const</span> Point &amp;a)<span class="keyword">const</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> x &lt; a.x;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">double</span> <span class="title">dist</span><span class="params">(<span class="keyword">const</span> Point &amp;p1, <span class="keyword">const</span> Point &amp;p2)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">sqrt</span>((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Point p[maxn], q[maxn];</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">ClosestPair</span><span class="params">(<span class="keyword">int</span> l, <span class="keyword">int</span> r)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(l == r)</span><br><span class="line">        <span class="keyword">return</span> inf;</span><br><span class="line">    <span class="keyword">int</span> mid = (l+r)&gt;&gt;<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">double</span> tx = p[mid].x;</span><br><span class="line">    <span class="keyword">int</span> tot = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">double</span> ret = min(ClosestPair(l, mid), ClosestPair(mid + <span class="number">1</span>, r));</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = l, j = mid + <span class="number">1</span>; (i &lt;= mid || j &lt;= r); ++i) &#123;</span><br><span class="line">        <span class="keyword">while</span>(j &lt;= r &amp;&amp; (p[i].y &gt; p[j].y || i &gt; mid)) &#123;</span><br><span class="line">            q[tot++] = p[j];</span><br><span class="line">            j++; <span class="comment">//归并按y排序</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(<span class="built_in">abs</span>(p[i].x - tx) &lt; ret &amp;&amp; i &lt;= mid) &#123; <span class="comment">//选择中间符合要求的点</span></span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> k = j - <span class="number">1</span>; k &gt; mid &amp;&amp; j - k &lt; <span class="number">3</span>; --k)</span><br><span class="line">                ret = min(ret, dist(p[i], p[k]));</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> k = j; k &lt;= r &amp;&amp; k-j &lt; <span class="number">2</span>; ++k)</span><br><span class="line">                ret = min(ret, dist(p[i], p[k]));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(i &lt;= mid)</span><br><span class="line">            q[tot++] = p[i];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = l, j = <span class="number">0</span>; i &lt;= r; ++i, ++j)</span><br><span class="line">        p[i] = q[j];</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="7-旋转卡壳（O-nlogn-解决平面最远点对）"><a href="#7-旋转卡壳（O-nlogn-解决平面最远点对）" class="headerlink" title="7 旋转卡壳（O(nlogn)解决平面最远点对）"></a>7 旋转卡壳（O(nlogn)解决平面最远点对）</h1><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Dist2</span><span class="params">(Point p1, Point p2)</span> </span>&#123; <span class="comment">//计算距离的平方</span></span><br><span class="line">    <span class="keyword">double</span> ret = Dot(p1 - p2, p1 - p2);</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">RotatingCalipers</span><span class="params">(Point* ch, <span class="keyword">int</span> m)</span> </span>&#123;<span class="comment">//返回平面最大距离的平方</span></span><br><span class="line">    <span class="keyword">if</span>(m == <span class="number">1</span>) <span class="keyword">return</span> <span class="number">0.0</span>;</span><br><span class="line">    <span class="keyword">if</span>(m == <span class="number">2</span>) <span class="keyword">return</span> Dist2(ch[<span class="number">0</span>], ch[<span class="number">1</span>]);</span><br><span class="line">    <span class="keyword">double</span> ret = <span class="number">0.0</span>;</span><br><span class="line">    ch[m] = ch[<span class="number">0</span>];</span><br><span class="line">    <span class="keyword">int</span> j = <span class="number">2</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m; ++i) &#123;</span><br><span class="line">        <span class="keyword">while</span>(Cross(ch[i + <span class="number">1</span>] - ch[i], ch[j] - ch[i]) &lt; Cross(ch[i + <span class="number">1</span>] - ch[i], ch[j + <span class="number">1</span>] - ch[i]))</span><br><span class="line">            j = (j + <span class="number">1</span>)%m;</span><br><span class="line">        ret = max(ret, max(Dist2(ch[j], ch[i]), Dist2(ch[j], ch[i + <span class="number">1</span>])));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="8-三点确定外接圆圆心坐标"><a href="#8-三点确定外接圆圆心坐标" class="headerlink" title="8 三点确定外接圆圆心坐标"></a>8 三点确定外接圆圆心坐标</h1><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span> &#123;</span></span><br><span class="line">    <span class="keyword">double</span> x,y;</span><br><span class="line">    Point(<span class="keyword">double</span> x = <span class="number">0</span>, <span class="keyword">double</span> y = <span class="number">0</span>):x(x),y(y)&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="function">Point <span class="title">Excenter</span><span class="params">(Point a, Point b, Point c)</span></span>&#123;</span><br><span class="line">    <span class="keyword">double</span> a1 = b.x - a.x;</span><br><span class="line">    <span class="keyword">double</span> b1 = b.y - a.y;</span><br><span class="line">    <span class="keyword">double</span> c1 = (a1*a1 + b1*b1)/<span class="number">2</span>;</span><br><span class="line">    <span class="keyword">double</span> a2 = c.x - a.x;</span><br><span class="line">    <span class="keyword">double</span> b2 = c.y - a.y;</span><br><span class="line">    <span class="keyword">double</span> c2 = (a2*a2 + b2*b2)/<span class="number">2</span>;</span><br><span class="line">    <span class="keyword">double</span> d = a1*b2 - a2*b1;</span><br><span class="line">    <span class="keyword">return</span> Point(a.x + (c1*b2 - c2*b1)/d, a.y + (a1*c2 - a2*c1)/d);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/ACM/" rel="tag"># ACM</a>
          
            <a href="/tags/计算几何/" rel="tag"># 计算几何</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/20190211/ACM几何基础篇/" rel="next" title="ACM几何基础篇">
                <i class="fa fa-chevron-left"></i> ACM几何基础篇
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/20190214/CodeForces-893-Educational-Codeforces-Round-33/" rel="prev" title="CodeForces - 893 Educational Codeforces Round 33">
                CodeForces - 893 Educational Codeforces Round 33 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  
    <div class="comments" id="comments">
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/star.jpg" alt="linxi">
            
              <p class="site-author-name" itemprop="name">linxi</p>
              <p class="site-description motion-element" itemprop="description">stay hungry, stay foolish</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">15</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">9</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">13</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.csdn.net/linxilinxilinxi" title="https://blog.csdn.net/linxilinxilinxi" rel="noopener" target="_blank">My CSDN</a>
                  </li>
                
              </ul>
            </div>
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-前言"><span class="nav-number">1.</span> <span class="nav-text">1 前言</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1-计算几何算法"><span class="nav-number">1.1.</span> <span class="nav-text">1.1 计算几何算法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-计算几何题目特点及要领"><span class="nav-number">1.2.</span> <span class="nav-text">1.2 计算几何题目特点及要领</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-预备知识"><span class="nav-number">1.3.</span> <span class="nav-text">1.3 预备知识</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-凸包"><span class="nav-number">2.</span> <span class="nav-text">2 凸包</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1-定义"><span class="nav-number">2.1.</span> <span class="nav-text">2.1 定义</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-1-凸多边形"><span class="nav-number">2.1.1.</span> <span class="nav-text">2.1.1 凸多边形</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-2-凸包"><span class="nav-number">2.1.2.</span> <span class="nav-text">2.1.2 凸包</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2-颜料配色问题"><span class="nav-number">2.2.</span> <span class="nav-text">2.2 颜料配色问题</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-1-问题描述"><span class="nav-number">2.2.1.</span> <span class="nav-text">2.2.1 问题描述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-2-问题简化"><span class="nav-number">2.2.2.</span> <span class="nav-text">2.2.2 问题简化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-3-问题抽象"><span class="nav-number">2.2.3.</span> <span class="nav-text">2.2.3 问题抽象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-4-数学抽象"><span class="nav-number">2.2.4.</span> <span class="nav-text">2.2.4 数学抽象</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-4-1-Convex-Combination-And-Affine-Combination"><span class="nav-number">2.2.4.1.</span> <span class="nav-text">2.2.4.1 Convex Combination And Affine Combination</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-4-2-区别与联系"><span class="nav-number">2.2.4.2.</span> <span class="nav-text">2.2.4.2 区别与联系</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-3-构造凸包的初步尝试"><span class="nav-number">2.3.</span> <span class="nav-text">2.3 构造凸包的初步尝试</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-1-转化思想"><span class="nav-number">2.3.1.</span> <span class="nav-text">2.3.1 转化思想</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-2-极性-Extremity"><span class="nav-number">2.3.2.</span> <span class="nav-text">2.3.2 极性 Extremity</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-3-基于极点的凸包构造算法"><span class="nav-number">2.3.3.</span> <span class="nav-text">2.3.3 基于极点的凸包构造算法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-4-基于极边的凸包构造算法"><span class="nav-number">2.3.4.</span> <span class="nav-text">2.3.4 基于极边的凸包构造算法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-4-1-极边"><span class="nav-number">2.3.4.1.</span> <span class="nav-text">2.3.4.1 极边</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-4-2-尝试实现"><span class="nav-number">2.3.4.2.</span> <span class="nav-text">2.3.4.2 尝试实现</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-4-更进一步"><span class="nav-number">2.4.</span> <span class="nav-text">2.4 更进一步</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-1-减而治之"><span class="nav-number">2.4.1.</span> <span class="nav-text">2.4.1 减而治之</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-2-转向形式"><span class="nav-number">2.4.2.</span> <span class="nav-text">2.4.2 转向形式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-3-优点与劣势"><span class="nav-number">2.4.3.</span> <span class="nav-text">2.4.3 优点与劣势</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-5-求解凸包的高效算法"><span class="nav-number">2.5.</span> <span class="nav-text">2.5 求解凸包的高效算法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-1-Jarvis-March的步进算法"><span class="nav-number">2.5.1.</span> <span class="nav-text">2.5.1 Jarvis March的步进算法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-1-1-思路分析"><span class="nav-number">2.5.1.1.</span> <span class="nav-text">2.5.1.1 思路分析</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-1-2-时间复杂度"><span class="nav-number">2.5.1.2.</span> <span class="nav-text">2.5.1.2 时间复杂度</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-1-3-注意"><span class="nav-number">2.5.1.3.</span> <span class="nav-text">2.5.1.3 注意</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-2-Graham-Scan"><span class="nav-number">2.5.2.</span> <span class="nav-text">2.5.2 Graham Scan</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-2-1-预处理"><span class="nav-number">2.5.2.1.</span> <span class="nav-text">2.5.2.1 预处理</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-2-2-具体步骤"><span class="nav-number">2.5.2.2.</span> <span class="nav-text">2.5.2.2 具体步骤</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-2-3-时间复杂度"><span class="nav-number">2.5.2.3.</span> <span class="nav-text">2.5.2.3 时间复杂度</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-2-4-代码实现"><span class="nav-number">2.5.2.4.</span> <span class="nav-text">2.5.2.4 代码实现</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-3-Andrew算法"><span class="nav-number">2.5.3.</span> <span class="nav-text">2.5.3 Andrew算法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-3-1-由来"><span class="nav-number">2.5.3.1.</span> <span class="nav-text">2.5.3.1 由来</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-3-2-预处理"><span class="nav-number">2.5.3.2.</span> <span class="nav-text">2.5.3.2 预处理</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-3-3-算法步骤"><span class="nav-number">2.5.3.3.</span> <span class="nav-text">2.5.3.3 算法步骤</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-3-4-时间复杂度"><span class="nav-number">2.5.3.4.</span> <span class="nav-text">2.5.3.4 时间复杂度</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-3-5-代码实现"><span class="nav-number">2.5.3.5.</span> <span class="nav-text">2.5.3.5 代码实现</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-4-分治法求解凸包"><span class="nav-number">2.5.4.</span> <span class="nav-text">2.5.4 分治法求解凸包</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-5-Melkman算法"><span class="nav-number">2.5.5.</span> <span class="nav-text">2.5.5 Melkman算法</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-离散化"><span class="nav-number">3.</span> <span class="nav-text">3 离散化</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#3-1-概述"><span class="nav-number">3.1.</span> <span class="nav-text">3.1 概述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-2-基本思想"><span class="nav-number">3.2.</span> <span class="nav-text">3.2 基本思想</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-3-例题：区域的个数"><span class="nav-number">3.3.</span> <span class="nav-text">3.3 例题：区域的个数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-1-题目描述"><span class="nav-number">3.3.1.</span> <span class="nav-text">3.3.1 题目描述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-2-思路分析"><span class="nav-number">3.3.2.</span> <span class="nav-text">3.3.2 思路分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-3-代码实现"><span class="nav-number">3.3.3.</span> <span class="nav-text">3.3.3 代码实现</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4-扫描线法（平面扫描）"><span class="nav-number">4.</span> <span class="nav-text">4 扫描线法（平面扫描）</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#4-1-描述"><span class="nav-number">4.1.</span> <span class="nav-text">4.1 描述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-2-例题：矩形面积并"><span class="nav-number">4.2.</span> <span class="nav-text">4.2 例题：矩形面积并</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-1-题目描述"><span class="nav-number">4.2.1.</span> <span class="nav-text">4.2.1 题目描述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-2-思路分析"><span class="nav-number">4.2.2.</span> <span class="nav-text">4.2.2 思路分析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-3-寻找平面线段交点O-nlogn"><span class="nav-number">4.3.</span> <span class="nav-text">4.3 寻找平面线段交点O(nlogn)</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#5-半平面交"><span class="nav-number">5.</span> <span class="nav-text">5 半平面交</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#5-1-描述"><span class="nav-number">5.1.</span> <span class="nav-text">5.1 描述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-2-有向线段"><span class="nav-number">5.2.</span> <span class="nav-text">5.2 有向线段</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-3-半平面交的结果"><span class="nav-number">5.3.</span> <span class="nav-text">5.3 半平面交的结果</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-4-计算方法"><span class="nav-number">5.4.</span> <span class="nav-text">5.4 计算方法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-4-1-增量法"><span class="nav-number">5.4.1.</span> <span class="nav-text">5.4.1 增量法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-4-2-切割方法"><span class="nav-number">5.4.2.</span> <span class="nav-text">5.4.2 切割方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-4-3-时间复杂度"><span class="nav-number">5.4.3.</span> <span class="nav-text">5.4.3 时间复杂度</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-4-4-代码实现"><span class="nav-number">5.4.4.</span> <span class="nav-text">5.4.4 代码实现</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#6-分治法解决平面最近点对（O-nlogn-）"><span class="nav-number">6.</span> <span class="nav-text">6 分治法解决平面最近点对（O(nlogn)）</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#7-旋转卡壳（O-nlogn-解决平面最远点对）"><span class="nav-number">7.</span> <span class="nav-text">7 旋转卡壳（O(nlogn)解决平面最远点对）</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#8-三点确定外接圆圆心坐标"><span class="nav-number">8.</span> <span class="nav-text">8 三点确定外接圆圆心坐标</span></a></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2019 – <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">linxi</span>

  

  
</div>


  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.8.0</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> v7.0.0</div>





<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">全站共 65.5k 字</span>
</div>

        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>














  
    
    
  
  <script color="25,202,173" opacity="0.8" zindex="-1" count="200" src="/lib/canvas-nest/canvas-nest.min.js"></script>













  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/src/utils.js?v=7.0.0"></script>

  <script src="/js/src/motion.js?v=7.0.0"></script>



  
  


  <script src="/js/src/affix.js?v=7.0.0"></script>

  <script src="/js/src/schemes/pisces.js?v=7.0.0"></script>



  
  <script src="/js/src/scrollspy.js?v=7.0.0"></script>
<script src="/js/src/post-details.js?v=7.0.0"></script>



  


  <script src="/js/src/bootstrap.js?v=7.0.0"></script>



  
  

<script src="//cdn1.lncld.net/static/js/3.11.1/av-min.js"></script>



<script src="//unpkg.com/valine/dist/Valine.min.js"></script>

<script>
  var GUEST = ['nick', 'mail', 'link'];
  var guest = 'nick,mail,link';
  guest = guest.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#comments',
    verify: false,
    notify: false,
    appId: 'LwzdQrGG81bUwkUkAfdPexj1-gzGzoHsz',
    appKey: 'akTaTqH7XeeKc6CPRfB7vbDl',
    placeholder: 'Just go go',
    avatar: 'mm',
    meta: guest,
    pageSize: '10' || 10,
    visitor: false
  });
</script>




  


  
  <script>
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x"></i></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x"></i></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  
  
  <script>
    
    function addCount(Counter) {
      var $visitors = $('.leancloud_visitors');
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();

      Counter('get', '/classes/Counter', { where: JSON.stringify({ url }) })
        .done(function({ results }) {
          if (results.length > 0) {
            var counter = results[0];
            
            Counter('put', '/classes/Counter/' + counter.objectId, JSON.stringify({ time: { '__op': 'Increment', 'amount': 1 } }))
            
              .done(function() {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.time + 1);
              })
            
              .fail(function ({ responseJSON }) {
                console.log(`Failed to save Visitor num, with error message: ${responseJSON.error}`);
              })
          } else {
            
              Counter('post', '/classes/Counter', JSON.stringify({ title: title, url: url, time: 1 }))
                .done(function() {
                  var $element = $(document.getElementById(url));
                  $element.find('.leancloud-visitors-count').text(1);
                })
                .fail(function() {
                  console.log('Failed to create');
                });
            
          }
        })
        .fail(function ({ responseJSON }) {
          console.log(`LeanCloud Counter Error: ${responseJSON.code} ${responseJSON.error}`);
        });
    }
    

    $(function() {
      $.get('https://app-router.leancloud.cn/2/route?appId=' + 'LwzdQrGG81bUwkUkAfdPexj1-gzGzoHsz')
        .done(function({ api_server }) {
          var Counter = function(method, url, data) {
            return $.ajax({
              method: method,
              url: 'https://' + api_server + '/1.1' + url,
              headers: {
                'X-LC-Id': 'LwzdQrGG81bUwkUkAfdPexj1-gzGzoHsz',
                'X-LC-Key': 'akTaTqH7XeeKc6CPRfB7vbDl',
                'Content-Type': 'application/json',
              },
              data: data
            });
          };
          
            addCount(Counter);
          
        });
    });
  </script>



  

  
  

  
  

  
    
      <script type="text/x-mathjax-config">
  

  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true,
      skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    },
    TeX: {
      
      equationNumbers: {
        autoNumber: "AMS"
      }
    }
  });
</script>

<script type="text/x-mathjax-config">
  MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
      for (i = 0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>
<script src="//cdn.jsdelivr.net/npm/mathjax@2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

<style>
.MathJax_Display {
  overflow: auto hidden;
}
</style>

    
  


  

  

  

  

  

  

  

  

  

  

</body>
</html>

<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/clicklove.js"></script>

